package com.yt.sys.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.common.utils.Md5Utils;
import com.yt.sys.common.config.YmlConfig;
import com.yt.sys.mapper.SysUserMapper;
import com.yt.sys.models.bo.SysUserSaveBO;
import com.yt.sys.models.dto.LoginInfoDTO;
import com.yt.sys.models.dto.SysRoleDTO;
import com.yt.sys.models.dto.SysUserDTO;
import com.yt.sys.models.dto.SysUserRoleDTO;
import com.yt.sys.models.service.ISysUserService;
import com.yt.sys.models.vo.SysPermissionVO;
import com.yt.sys.models.vo.SysUserVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户信息控制器
 *
 * @author zhou
 * @Date 2025-09-03 14:25:55
 */
@DubboService
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUserDTO> implements ISysUserService {

    @Resource
    private YmlConfig ymlConfig;
    @Resource
    private LoginInfoServiceImpl loginInfoService;
    @Resource
    private SysRoleServiceImpl sysRoleService;
    @Resource
    private SysUserRoleServiceImpl sysUserRoleService;

    @Override
    public boolean add(SysUserSaveBO saveBO) {
        SysUserDTO dto = new SysUserDTO();
        BeanUtils.copyProperties(saveBO, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    @Transactional
    public SysUserDTO saveSysUser(SysUserSaveBO bo) {
        //1.系统用户信息保存
        SysUserDTO dto = new SysUserDTO();
        BeanUtils.copyProperties(bo, dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        this.insert(dto);

        //2.用户登录信息保持
        LoginInfoDTO loginInfoDTO = new LoginInfoDTO();
        //2.1主键设置
        Long loginId = ymlConfig.getPkId();
        loginInfoDTO.setId(loginId);
        //2.2账号设置
        loginInfoDTO.setAccount(bo.getPhone());
        //2.3关联用户id设置
        loginInfoDTO.setUserId(id);
        //2.4密码设置
        String salt = Md5Utils.createSalt();
        loginInfoDTO.setSalt(salt);
        loginInfoDTO.setPassword(Md5Utils.MD5Encode(bo.getPassword(), salt));
        loginInfoService.insert(loginInfoDTO);

        //3.系统用户角色关系表
        SysUserRoleDTO sysUserRoleDTO = new SysUserRoleDTO();
        //3.1设置主键
        Long roId = ymlConfig.getPkId();
        sysUserRoleDTO.setId(roId);
        //3.2设置user_id
        sysUserRoleDTO.setUserId(id);
        //3.3设置role_id
        Wrapper<SysRoleDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("role_name", bo.getRoleName());
        SysRoleDTO sysRoleDTO = sysRoleService.selectOne(wrapper);
        sysUserRoleDTO.setRoleId(sysRoleDTO.getId());

        sysUserRoleService.insert(sysUserRoleDTO);
        return dto;
    }

    @Override
    public boolean updateSysUser(SysUserSaveBO bo) {
        SysUserDTO userDTO = new SysUserDTO();
        BeanUtils.copyProperties(bo, userDTO);
        boolean userUpdate = this.updateById(userDTO);
        //修改角色信息，删除原来用户角色信息，新增一条用户角色信息
        Wrapper<SysUserRoleDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id", bo.getId());
        sysUserRoleService.delete(wrapper);
        SysUserRoleDTO sysUserRoleDTO = new SysUserRoleDTO();
        //3.1设置主键
        Long roId = ymlConfig.getPkId();
        sysUserRoleDTO.setId(roId);
        //3.2设置user_id
        sysUserRoleDTO.setUserId(bo.getId());
        //3.3设置role_id
        Wrapper<SysRoleDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("role_name", bo.getRoleName());
        SysRoleDTO sysRoleDTO = sysRoleService.selectOne(wrapper1);
        sysUserRoleDTO.setRoleId(sysRoleDTO.getId());
        sysUserRoleService.insert(sysUserRoleDTO);
        return userUpdate;
    }

    @Override
    public List<SysUserVO> querySysUsers() {
        Wrapper<SysUserDTO> wrapper = new EntityWrapper<>();
        List<SysUserDTO> sysUserDTO = this.selectList(wrapper);
        if (sysUserDTO == null || sysUserDTO.size() == 0) {
            return null;
        }
        List<SysUserVO> sysUserVOS = new ArrayList<>();
        for (SysUserDTO sysUserDTO1 : sysUserDTO) {
            SysUserVO sysUserVO = new SysUserVO();
            BeanUtils.copyProperties(sysUserDTO1, sysUserVO);
            //角色名
            Wrapper<SysUserRoleDTO> wrapper1 = new EntityWrapper<>();
            wrapper1.eq("user_id", sysUserDTO1.getId());
            SysUserRoleDTO sysUserRoleDTO1 = sysUserRoleService.selectOne(wrapper1);
            if (sysUserRoleDTO1 == null) {
                sysUserVO.setRoleName("null");
                sysUserVOS.add(sysUserVO);
                continue;
            }
            Wrapper<SysRoleDTO> wrapper2 = new EntityWrapper<>();
            wrapper2.eq("id", sysUserRoleDTO1.getRoleId());
            SysRoleDTO sysRoleDTO2 = sysRoleService.selectOne(wrapper2);
            sysUserVO.setRoleName(sysRoleDTO2.getRoleName());
            sysUserVOS.add(sysUserVO);
        }
        return sysUserVOS;
    }

    @Override
    public List<SysPermissionVO> queryPermission(Long id) {
        List<SysPermissionVO> sysPermissionVO = baseMapper.queryPermission(id);
        return sysPermissionVO;
    }

    @Override
    public List<SysUserVO> queryEmp() {
        List<SysUserVO> sysUserVOS = baseMapper.queryEmployee();
        return sysUserVOS;
    }
}
